package com.mxpio.erp.equipment.job;

import cn.hutool.core.date.DateUtil;
import com.mxpio.erp.common.enums.CommonEnums;
import com.mxpio.erp.common.enums.EquipmentEnums;
import com.mxpio.erp.common.equipment.entity.EquipmentCalibration;
import com.mxpio.erp.common.equipment.entity.EquipmentEqpBasics;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.system.service.SnRuleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author : wpp
 * @Datetime : 2023/12/21 11:53
 * @Desc :
 * @Modor :  Modifytime:
 * @modDesc :
 */
@Slf4j
@Component
public class CalibrationTaskJob implements Job {

    private String parameter;

    @Autowired
    private SnRuleService snRuleService;

    public void setParameter(String parameter) {
        this.parameter = parameter;
    }
    @Override
    @Transactional
    public void execute(JobExecutionContext context) throws JobExecutionException {
        List<EquipmentEqpBasics> eqpBasics = JpaUtil.linq(EquipmentEqpBasics.class)
                .equal("isCalibrate", Integer.parseInt(CommonEnums.YesNo.YES.getCode()))
                .isNotNull("calibrateAdvance").list();
        String today = DateUtil.formatDate(new Date());//今天
        log.info("检定任务【"+today+"】job执行开始："+DateUtil.formatDateTime(new Date()));
        if (!eqpBasics.isEmpty()){
            String  patern= "QCI#####";
            for (EquipmentEqpBasics basics:eqpBasics){
                Integer calibrateCycle = basics.getCalibrateCycle();
                Date lastCalibrateDate = basics.getLastCalibrateDate();
                Integer calibrateAdvance = basics.getCalibrateAdvance();//检定任务生成提前期
                if (lastCalibrateDate==null||calibrateAdvance==null){
                    log.error("设备台账【"+basics.getBasicsCode()+"】基础数据【上次检定日期】或者【检定提前期(天)】未维护");
                    continue;
                }
                Date calibrateDate = DateUtils.addDays(lastCalibrateDate, calibrateCycle);//校验到期日(检定任务 检定日期)
                Date genarateTaskDate = DateUtils.addDays(calibrateDate, 0 - calibrateAdvance);//检定任务生成日期
                if (today.equals(DateUtil.formatDate(genarateTaskDate))){//等于今天
                    Long count = JpaUtil.linq(EquipmentCalibration.class)
                            .equal("targetId", basics.getBasicsCode())
                            .equal("calibrationDate", DateUtil.formatDate(calibrateDate))
                            .equal("calibrationType", EquipmentEnums.CalibrationType.PLAN.getCode()).count();//判断任务是否生成
                    if (count!=null&&count>0){
                        log.error("检定任务已存在:"+basics.getBasicsCode()+":"+DateUtil.formatDate(calibrateDate));
                        continue;//任务已生成
                    }
                    EquipmentCalibration calibration = new EquipmentCalibration();
                    String bizNo = (String) snRuleService.execute(patern, null);
                    calibration.setCalCode(bizNo);
                    calibration.setEqpCode(basics.getEqpCode());
                    calibration.setTargetId(basics.getBasicsCode());
                    calibration.setTargetName(basics.getEqpName());
                    calibration.setSpecType(basics.getSpecType());
                    calibration.setUseDeptId(basics.getUseDeptId());
                    calibration.setEqpTypeId(basics.getEqpTypeId());
                    calibration.setLayAddr(basics.getLayAddr());
                    calibration.setCalibrationType(EquipmentEnums.CalibrationType.PLAN.getCode());//计划
                    calibration.setCalibrationDate(calibrateDate);
                    calibration.setExecutionStatus(EquipmentEnums.Calibration.PLAN.getCode());//默认计划状态
                    JpaUtil.save(calibration);//保存检定任务
                }
            }
        }
        log.info("检定任务【"+today+"】job执行结束："+DateUtil.formatDateTime(new Date()));
    }
}
